package com.cloudwise.controller;

import com.alibaba.fastjson.JSON;
import com.cloudwise.service.UserInfoService;
import com.cloudwise.service.impl.UserInfoServiceImpl;
import com.cloudwise.util.MyReturn;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2021/9/13 10:09
 * @description：登录控制器
 * @modified By：
 * @version: 1.0
 */
@WebServlet( urlPatterns = "/loginAjaxServlet")
public class LoginAjaxServlet extends HttpServlet {
    UserInfoService userInfoService= new UserInfoServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //开始写业务方法
        Map<String, Object> user = userInfoService.findUserByUserNameAndPassword(username, password);
        MyReturn myReturn= new MyReturn();

        if(user!=null){
            //登录成功
            myReturn.setCode(0);

        }else
        {
            //登录失败
            myReturn.setCode(1);
            myReturn.setMsg("登录的用户名或者密码错误");
        }
        //设置响应数据的格式
        resp.setContentType("application/json");
        //将实体对象转换成json字符串回传
        String jsonString = JSON.toJSONString(myReturn);
        resp.getWriter().write(jsonString);
    }

}
